package top.infopub.mgr.sys.domain;


import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.beanutils.BeanUtils;

import top.infopub.model.sys.City;
import top.infopub.model.sys.Province;


public class ProvinceDomain extends Province {

    private static final long serialVersionUID = -3067574116601228428L;

    private Set<City> citys = new TreeSet<City>(new CityComparator());

    public void addCity(City t) {
        boolean addFlag = true;
        for (City city : citys) {
            if (city.getId().equalsIgnoreCase(t.getId())) {
                addFlag = false;
                break;
            }
        }
        if (addFlag) citys.add(t);
    }

    public Set<City> getListCitys() {
        Set<City> _tmp = new TreeSet<City>(new CityComparator());
        for (City city : citys) {
            City dest = new City();
            try {
                BeanUtils.copyProperties(dest, city);
                _tmp.add(dest);
            }
            catch (Exception e) {
                //do nothing
            }
        }
        return _tmp;
    }

    class CityComparator implements Comparator<City> {
        @Override
        public int compare(City o1, City o2) {
            if (o1.getIsort() == null || o2.getIsort() == null) {
                return -1;
            }
            if (o1.getIsort() > o2.getIsort()) {
                return 1;
            }
            else {
                return -1;
            }
        }
    }

}
